﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using Sirenix.OdinInspector;

public class ScriptHeadWindow
{
    [Title("Head")]
    [LabelWidth(150), Multiline]
    public string _Description = "代码头信息\n在创建脚本文件时下面字段将写入脚步文件内\n下面字段保存在Prefa内，在不删除Prefab前提字段将会保存下去";
    [OnValueChanged("OnEnableChange"), LabelText("是否启用")] public bool ScriptHeadEnable = false;
    [Title("代码头信息")]
    [OnValueChanged("OnAuthorChange"), LabelWidth(150)] public string 作者;
    [OnValueChanged("OnBlogChange"), LabelWidth(150)] public string 博客;
    [OnValueChanged("OnGiteeChange"), LabelWidth(150)] public string Git;
    [OnValueChanged("OnEmailChange"), LabelWidth(150)] public string 邮箱;
    [OnValueChanged("OnDescriptionChange"), LabelWidth(150)] public string 描述;

    #region 字段
    public static string Author = "XXY";
    public static string Blog = "https://www.cnblogs.com/jzyl";
    public static string Gitee = "https://gitee.com/xiaobaichong";
    public static string Email = "1047185209@QQ.com";
    public static string Description = "描述";
    #endregion

    public ScriptHeadWindow()
    {
        作者 = EditorPrefs.GetString("作者", ScriptHeadWindow.Author);
        博客 = EditorPrefs.GetString("博客", ScriptHeadWindow.Blog);
        Git = EditorPrefs.GetString("Git", ScriptHeadWindow.Gitee);
        邮箱 = EditorPrefs.GetString("邮箱", ScriptHeadWindow.Email);
        描述 = EditorPrefs.GetString("描述", ScriptHeadWindow.Description);
        ScriptHeadEnable = EditorPrefs.GetInt("ScriptHeadEnable", 0) == 1;
    }

    private void OnAuthorChange()
    {
        EditorPrefs.SetString("作者", 作者);
    }
    private void OnBlogChange()
    {
        EditorPrefs.SetString("博客", 博客);
    }
    private void OnGiteeChange()
    {
        EditorPrefs.SetString("Git", Git);
    }
    private void OnEmailChange()
    {
        EditorPrefs.SetString("邮箱", 邮箱);
    }
    private void OnDescriptionChange()
    {
        EditorPrefs.SetString("描述", 描述);
    }
    private void OnEnableChange()
    {
        EditorPrefs.SetInt("ScriptHeadEnable", ScriptHeadEnable ? 1 : 0);
    }
}
/// <summary>
/// 创建脚本头信息
/// </summary>
public class ScriptHeadComment : UnityEditor.AssetModificationProcessor
{
    /// <summary>
    /// Unity 在即将创建未导入的资源（例如，.meta 文件）时调用此方法。
    /// </summary>
    /// <param name="filePath"></param>
    private static void OnWillCreateAsset(string filePath)
    {
        // 是C# 脚本文件
        // ScriptHeadEnable 已启用
        if (filePath.EndsWith(".cs.meta") && EditorPrefs.GetInt("ScriptHeadEnable", 0) == 1)
        {
            filePath = filePath.Substring(0, filePath.Length - 5);
            WriteHeadComment(filePath);
        }
    }
    private static void WriteHeadComment(string filePath)
    {
        /************************************************
         * Author       :   xxy
         * Blog         :   https://www.cnblogs.com/jzyl
         * Gitee        :   https://gitee.com/xiaobaichong
         * Email        :   1047185209@QQ.com
         * FileName     :   ******.cs
         * CreateData   :   2020/3/3 20:30
         * UnityVersion :   2020.3.33
         * Description  :   描述
         ************************************************/
        string allText = File.ReadAllText(filePath);
        string title = "" +
            "/************************************************" + "\n" +
            " * Author       :   " + EditorPrefs.GetString("作者", ScriptHeadWindow.Author) + "\n" +
            " * Blog         :   " + EditorPrefs.GetString("博客", ScriptHeadWindow.Blog) + "\n" +
            " * Gitee        :   " + EditorPrefs.GetString("Git", ScriptHeadWindow.Gitee) + "\n" +
            " * Email        :   " + EditorPrefs.GetString("邮箱", ScriptHeadWindow.Email) + "\n" +
            " * FileName     :   " + Path.GetFileName(filePath) + "\n" +
            " * CreateData   :   " + DateTime.Now.ToString() + "\n" +
            " * UnityVersion :   " + Application.unityVersion + "\n" +
            " * Description  :   " + EditorPrefs.GetString("描述", ScriptHeadWindow.Description) + "\n" +
            "************************************************/" + "\n\n";
        File.WriteAllText(filePath, title + allText);
    }
}
